package com.ev.framework.utils;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

@Slf4j
public class PinYinUtil {
    /**
     * 获取中文转拼音
     * @param inputString 中文
     * @return 你好 NIHAO
     */
    public static String getPinYin(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        char[] input = inputString.trim().toCharArray();
        StringBuilder output = new StringBuilder();
        try {
            for (char c : input) {
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {  //判断字符是否是中文
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    output.append(temp[0]);
                } else {
                    output.append(c);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        log.info("转换的字母大写为:{}", output.toString());
        return output.toString();
    }
    /**
     * 获取拼音带下划线分隔
     * @param inputString 中文
     * @return 你好 NI_HAO
     */
    public static String getPinYinHasLine(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        char[] input = inputString.trim().toCharArray();
        StringBuilder output = new StringBuilder();
        try {
            for (int i = 0; i < input.length; i++) {
                if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {  //判断字符是否是中文
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    output.append(temp[0]);
                    if(i==input.length-1){
                        continue;
                    }
                    output.append("_");
                } else {
                    output.append(input[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        log.info("转换的字母大写为:{}", output.toString());
        return output.toString();
    }

    /**
     * 取出拼音中第一个字母,一般第一个字母的使用时比较常见的
     * @param chines 中文
     * @return 你好 NH
     */
    public static String converterToFirstSpell(String chines) {
        StringBuilder pinyinName = new StringBuilder();
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaulFormat = new HanyuPinyinOutputFormat();
        defaulFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaulFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        defaulFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        for (char c : nameChar) {
            if (c > 128) {
                try {
                    pinyinName.append(PinyinHelper.toHanyuPinyinStringArray(c, defaulFormat)[0].charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination ex) {
                    ex.printStackTrace();
                }
            } else {
                pinyinName.append(c);
            }
        }
        log.info("转换的首字母大写为:{}", pinyinName.toString());
        return pinyinName.toString();
    }
}
